
package com.ami.kvm.jviewer.gui;

import java.awt.event.KeyEvent;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.swing.JMenuItem;

import com.ami.kvm.jviewer.Debug;

public class AddMacro {
	private ByteBuffer macroBuffer;
	public static final int MACRO_COUNT = 20;
	public static final int SINGLE_MACRO_LENGTH = 6;
	public static final int SIZE_OF_MACRO = 960;
	protected HashMap<Integer, Integer> standardMapInvert;
	protected HashMap<Integer, Integer> keypadMapInvert;
	protected HashMap<Integer, Integer> leftMapInvert;
	protected HashMap<Integer, Integer> rightMapInvert;
	protected HashMap<String, String> macroMap;
	public static final String UNKNOWN_KEY = "Unknown";

	public AddMacro(ByteBuffer paramByteBuffer) {
		this.macroBuffer = paramByteBuffer;
		if (this.macroMap == null) {
			this.macroMap = new HashMap();
		}
	}

	public void parseDataToMenu() {
		this.macroBuffer.position(0);
		this.macroBuffer.order(ByteOrder.BIG_ENDIAN);

		for (int i = 0; i < 20; i++) {
			String str1 = new String();
			String str2 = new String();

			for (int j = 0; j < 6; j++) {
				try {
					int k = this.macroBuffer.getInt();
					int m = this.macroBuffer.getInt();

					if ((k != 0) && (m != 0)) {
						String str3 = KeyEvent.getKeyText(k);

						if (str3.startsWith("Unknown")) {
							str3 = "";
							Debug.out.println("UNKNOWN Code : " + k);
							break;
						}
						if (str3.equals("NumPad +")) {
							str3 = "NumPad Plus";
						}
						if (str1.length() != 0) {
							str1 = str1.concat("+");
						}
						if (m != 1) {
							if (m == 2) {
								str3 = str3.concat("(Left)");
							} else if (m == 3) {
								str3 = str3.concat("(Right)");
							}
						}
						str1 = str1.concat(str3);

						String str4 = new String(Integer.toString(k));
						String str5 = new String(Integer.toString(m));
						if (str2.length() != 0)
							str2 = str2.concat("+");
						str2 = str2.concat(str4);
						str2 = str2.concat("+" + str5);
					}
				} catch (BufferOverflowException localBufferOverflowException) {
					Debug.out.println(localBufferOverflowException);
				} catch (BufferUnderflowException localBufferUnderflowException) {
					Debug.out.println(localBufferUnderflowException);
				}
			}
			if ((str1.length() != 0) && (str2.length() != 0))
				this.macroMap.put(str1, str2);
		}
		removeMacroMenu();
		removeToolbarMacro();
		addMacroMenu(this.macroMap);
		addToolbarMacro(this.macroMap);
	}

	public void addMacroMenu(HashMap<String, String> paramHashMap) {
		if (paramHashMap.size() > 0) {
			Set localSet = this.macroMap.entrySet();
			Iterator localIterator = localSet.iterator();
			JViewerApp.getInstance().getJVMenu().getMacroSubMenu().addSeparator();
			String str;
			for (; localIterator.hasNext();

					JVMenu.m_menuItems_setenabled.put(str, Boolean.valueOf(false))) {
				Map.Entry localEntry = (Map.Entry) localIterator.next();
				str = (String) localEntry.getKey();
				JMenuItem localJMenuItem = new JMenuItem(str);
				localJMenuItem.addActionListener(JViewerApp.getInstance().getJVMenu().m_menuListener);
				JViewerApp.getInstance().getJVMenu();
				localJMenuItem.addMouseListener(JVMenu.m_menuStatus);
				localJMenuItem.setActionCommand("HK_" + str);
				JViewerApp.getInstance().getJVMenu().getMacroSubMenu().add(localJMenuItem, 2);
				JViewerApp.getInstance().getJVMenu().m_menuItems.put(str, localJMenuItem);
				JViewerApp.getInstance().getJVMenu();
				JVMenu.m_menuItems_setenabled.put(str, Boolean.valueOf(true));

				if ((KVMSharing.KVM_REQ_GIVEN == 2) || (JViewerApp.getInstance().isM_userPause())) {
					localJMenuItem.setEnabled(false);
					JViewerApp.getInstance().getJVMenu();
				}
			}
		}
	}

	public void addToolbarMacro(HashMap<String, String> paramHashMap) {
		if (paramHashMap.size() > 0) {
			Set localSet = this.macroMap.entrySet();
			Iterator localIterator = localSet.iterator();
			JViewerApp.getInstance().getM_wndFrame().getToolbar().getM_popupMenu().addSeparator();
			while (localIterator.hasNext()) {
				Map.Entry localEntry = (Map.Entry) localIterator.next();
				String str = (String) localEntry.getKey();
				JViewerApp.getInstance().getM_wndFrame().getToolbar().addHotkeyPoupMenuItem(str);
			}
		}
	}

	public void removeMacroMenu() {
		JViewerApp.getInstance().getJVMenu().getMacroSubMenu().removeAll();
		JMenuItem localJMenuItem = JViewerApp.getInstance().getJVMenu().getMenuItem("Add Hotkeys");
		JViewerApp.getInstance().getJVMenu().getMacroSubMenu().add(localJMenuItem);
	}

	public void removeToolbarMacro() {
		JViewerApp.getInstance().getM_wndFrame().getToolbar().removeHotkeyPoupMenuItem();
	}

	public void parseKeycodeSendBMC() {
		byte[] arrayOfByte = new byte['π'];
		Arrays.fill(arrayOfByte, (byte) 0);
		ByteBuffer localByteBuffer = ByteBuffer.wrap(arrayOfByte);
		Set localSet = this.macroMap.entrySet();
		Iterator localIterator = localSet.iterator();
		int i = 1;
		while (localIterator.hasNext()) {
			Map.Entry localEntry = (Map.Entry) localIterator.next();
			String str = (String) localEntry.getValue();
			String[] arrayOfString = str.split("[+]");

			for (int j = 0; j < arrayOfString.length; j++) {
				if (j < arrayOfString.length) {
					int k = Integer.parseInt(arrayOfString[j]);
					localByteBuffer.putInt(k);
				}
			}

			localByteBuffer.position(i * 12 * 4);
			i++;
		}
		JViewerApp.getInstance().getKVMClient().sendUserMacroData(arrayOfByte);
	}

	public ByteBuffer getMacroBuffer() {
		return this.macroBuffer;
	}

	public void setMacroBuffer(ByteBuffer paramByteBuffer) {
		this.macroBuffer = paramByteBuffer;
	}

	public HashMap<String, String> getMacroMap() {
		return this.macroMap;
	}

	public void setMacroMap(HashMap<String, String> paramHashMap) {
		this.macroMap = paramHashMap;
	}
}

/*
 * Location:
 * D:\Users\tianlin\Downloads\JViewer.jar!\com\ami\kvm\jviewer\gui\AddMacro.
 * class Java compiler version: 5 (49.0) JD-Core Version: 0.7.1
 */